package com.zsg.webview.Adapter;

import android.content.Context;
import android.text.Html;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zsg.webview.Activities.MyLinearLayout;
import com.zsg.webview.Config;
import com.zsg.webview.R;
import com.zsg.webview.Util.Tools;
import com.zsg.webview.WeiBoBaseClass.Comment;

import java.util.ArrayList;

/**
 * 显示评论信息的适配器  适配layout
 * Created by zzc on 2015/11/14.
 */
public class CommantAdapter extends BaseAdapter {
    public ArrayList<Comment> data;
    Context context;
    LayoutInflater inflater;
    MyLinearLayout layout;

    public CommantAdapter(Context context) {
        this.context = context;
        this.data = new ArrayList<>();
        inflater = LayoutInflater.from(context);
    }

    public void registerLayout(MyLinearLayout layout) {
        this.layout = layout;
        layout.initAdapter(this);
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        layout.initView();
    }

    public void updateData(ArrayList<Comment> temp, int type) {
        if (type == Config.TYPE_GETMORE)
            data.addAll(temp);
        else if (type == Config.TYPE_REFRESH){
            data.clear();
            data.addAll(temp);
        }
    }

    @Override
    public int getCount() {
        return data.size() +1;
    }

    @Override
    public Comment getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (position == getCount() - 1) {
            if(convertView==null) {
                convertView = inflater.inflate(R.layout.item_waitload, parent);
                TextView textView = (TextView) convertView.findViewById(R.id.textView);
                textView.setText("上划加载更多评论");
                convertView.findViewById(R.id.imageView).setVisibility(View.INVISIBLE);
                return convertView;
            }else
                return convertView;
        }

        CommentHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_comment, parent);
            holder = new CommentHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (CommentHolder) convertView.getTag();
        }

        Comment comment = data.get(position);
        Picasso.with(context)
                .load(comment.user.profile_image_url)
                .into(holder.icon);
        holder.name.setText(comment.user.screen_name);
        holder.message.setText(Tools.changeTextToFace(context,
                Html.fromHtml(Tools.atBlue(comment.text))));
        holder.time.setText(comment.created_at);

        return convertView;
    }

    class CommentHolder {
        ImageView icon;
        TextView name;
        TextView time;
        TextView message;

        public CommentHolder(View view) {
            icon = (ImageView) view.findViewById(R.id.imageicon);
            name = (TextView) view.findViewById(R.id.name);
            message = (TextView) view.findViewById(R.id.message);
            time = (TextView) view.findViewById(R.id.time);
        }
    }
}
